<%@ page language="java" contentType="text/html; charset=UTF-8"
         pageEncoding="UTF-8" %>
<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>车辆管理</title>
    <link rel="stylesheet" type="text/css"
          href="${pageContext.request.contextPath}/js/jquery-easyui-1.4.1/themes/default/easyui.css"/>
    <link rel="stylesheet" type="text/css"
          href="${pageContext.request.contextPath}/js/jquery-easyui-1.4.1/themes/icon.css"/>
    <script type="text/javascript"
            src="${pageContext.request.contextPath}/js/jquery-easyui-1.4.1/jquery.min.js"></script>
    <script type="text/javascript"
            src="${pageContext.request.contextPath}/js/jquery-easyui-1.4.1/jquery.easyui.min.js"></script>
    <script type="text/javascript"
            src="${pageContext.request.contextPath}/js/jquery-easyui-1.4.1/locale/easyui-lang-zh_CN.js"></script>
    <script type="text/javascript" src="${pageContext.request.contextPath}/js/common.js"></script>
    <script type="text/javascript">

        /* 映射车队名称 */
        function formatTeamName(val, row) {
            return row.busTeam.teamName;
        }

        function formatSet(val, row) {
            if (val == 1) {
                return "运行";
            } else if (val == 0) {
                return "停运";
            }
        }

        function getSelectionsIds() {
            var busList = $("#busList");
            var sels = busList.datagrid("getSelections");
            var ids = [];
            for (var i in sels) {
                ids.push(sels[i].busId);
            }
            ids = ids.join(",");
            return ids;
        }

        //将时间戳转换为正常的时间格式

        function timestampToTime(timestamp) {
            var date = new Date(timestamp);//时间戳为10位需*1000，时间戳为13位的话不需乘1000
            var Y = date.getFullYear() + '-';
            var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';
            var D = date.getDate() + ' ';
            var h = date.getHours() + ':';
            var m = date.getMinutes() + ':';
            var s = date.getSeconds();
            return Y + M + D;
        };
        var toolbar = [{
            text: '车主姓名：<input id = "serach1" class="search" style="width:60px;"/>',
        }, {
            text: '车牌号：<input id = "serach2" class="search" style="width:80px;"/>',
        }, {
            text: '搜索',
            iconCls: 'icon-search',
            handler: function () {
                var driverName = $("#serach1").val();
                var busNumber = $("#serach2").val();
                $("#busList").datagrid("load", {
                    driverName: driverName,
                    busNumber: busNumber
                });
            }
        }, {
            text: '新增',
            iconCls: 'icon-add',
            handler: function () {
                $('#busAdd').window('open');
            }
        }, {
            text: '编辑',
            iconCls: 'icon-edit',
            handler: function () {
                var ids = getSelectionsIds();

                if (ids.length == 0) {
                    $.messager.alert('提示', '还未选中要修改的车辆');
                    return;
                } else if (ids.indexOf(',') > 0) {
                    $.messager.alert('提示', '选中多个修改的车辆');
                    return;
                }
                $.messager.confirm('确认', '确定要修改ID为' + ids + '的车辆吗？', function (r) {
                    if (r) {
                        $('#busUpdate').window({
                            onLoad: function () {
                                var busList = $("#busList");
                                /* 由于获取到的bus是一个数组，这里必须加上【0】 */
                                var bus = busList.datagrid("getSelections")[0];
                                $("#content").form("load", {
                                    busId: bus.busId,
                                    busNumber: bus.busNumber,
                                    teamName: bus.busTeam.teamName,
                                    lineName: bus.busLine.lineName,
                                    driverName: bus.driverName,
                                    driverPhone: bus.driverPhone,
                                    teamId: bus.teamId,
                                    lineId: bus.lineId,
                                    busStatus: bus.busStatus
                                });
                            }
                        }).window('open');
                    }
                });
            }
        }, {
            text: '删除',
            iconCls: 'icon-cancel',
            handler: function () {
                var busIds = getSelectionsIds();
                if (busIds.length == 0) {
                    $.messager.alert('提示', '未选中车辆!');
                    return;
                }
                $.messager.confirm('确认', '确定删除ID为 ' + busIds + ' 的车辆吗？', function (r) {
                    if (r) {
                        $.post("${pageContext.request.contextPath}/bus/delete", {'busIds': busIds}, function (data) {
                            // $.messager.alert('data',data.code);
                            if (data.code == 200) {
                                $.messager.alert('提示', '删除车辆成功!', undefined, function () {
                                    $("#busList").datagrid('reload');
                                });
                            } else {
                                $.messager.alert('提示', '删除车辆失败!', undefined, function () {
                                    $("#busList").datagrid('reload');
                                });
                            }
                        }, "json");
                    }
                });
            }
        }, '-', {
            text: '导出',
            iconCls: 'icon-remove',
            handler: function () {
				$.messager.alert("提示","敬请期待");
				// var optins = $("#busList").datagrid("getPager").data("pagination").options;
				// var page = optins.pageNumber;
				// var rows = optins.pageSize;
                // $("<form>").attr({
                //     "action": "/bus/export/excel",
                //     "method": "POST"
                // }).append("<input type='text' name='page' value='" + page + "'/>")
                //     .append("<input type='text' name='rows' value='" + rows + "'/>")
                //     .appendTo("body")
                //     .submit();
            }
        }];
    </script>
</head>
<body>
<table class="easyui-datagrid" id="busList"
       data-options="singleSelect:false,collapsible:true,pagination:true,url:'${pageContext.request.contextPath}/bus/all',method:'post',pageSize:10,toolbar:toolbar,pageList:[2,5,10],rownumbers:true">
    <thead>
    <tr>
        <th data-options="field:'ck',checkbox:true"></th>
        <th data-options="field:'busId',width:20" hidden="true">ID</th>
        <th data-options="field:'busNumber',width:100">车牌号</th>
        <th data-options="field:'teamName',width:200,formatter:formatTeamName">所属车队</th>
        <th data-options="field:'driverName',width:90,align:'right'">司机姓名</th>
        <th data-options="field:'driverPhone',width:120,align:'right'">司机电话</th>
        <th data-options="field:'busStatus',width:80,align:'center',formatter:formatSet">运行状态</th>
    </tr>
    </thead>
</table>
<div id="busAdd" class="easyui-window" title="新增车辆信息"
     data-options="modal:true,closed:true,iconCls:'icon-save',href:'${pageContext.request.contextPath}/to/bus-add'"
     style="width:550px;height:350px;padding:10px;top:80px">
    The window content.
</div>
<div id="busUpdate" class="easyui-window" title="修改车辆信息"
     data-options="model:true,closed:true,iconCls:'icon-update',href:'${pageContext.request.contextPath}/to/bus-update'"
     style="width:700px;height:400px;padding:10px;top:60px">
    The window context.
</div>

</body>
</html>